package com.app.songbump;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class BumpActivity extends Activity {
	
	String uid;
	AppPreferences appPrefs;
	TextView idText;
	Button bumpButton;
	EditText friendIdText;

	private BumpResponseReciever receiver;
	
	public class BumpResponseReciever extends BroadcastReceiver {
		   @Override
		    public void onReceive(Context context, Intent intent) {
		       	BumpActivity.this.finish();
		    }
	}
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bump);
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		idText = (TextView) this.findViewById(R.id.idView);
		bumpButton = (Button) this.findViewById(R.id.bumpButton);
		friendIdText = (EditText) this.findViewById(R.id.friendId);

		IntentFilter filter = new IntentFilter(SongBumpServer.BUMP_RESPONSE);
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		receiver = new BumpResponseReciever();
		registerReceiver(receiver, filter);
		
		appPrefs = new AppPreferences(getApplicationContext());
		uid = appPrefs.getUserId();
		idText.setText("My ID is " + uid);

		bumpButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d("debug","button clicked!");
				
				String friendId = friendIdText.getText().toString();
				
				Intent msgIntent = new Intent(BumpActivity.this, SongBumpServer.class);
				msgIntent.putExtra(SongBumpServer.ACTION,SongBumpServer.BUMP);
				msgIntent.putExtra("start",uid);
				msgIntent.putExtra("end",friendId);
				startService(msgIntent);
				
			}
		});
	}
	
	public void onDestroy(){
		super.onDestroy();
		unregisterReceiver(receiver);
	}

}
